//
//  PaiRecordViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/8/5.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "UserPaiPriceCell.h"
#import "MyPaiPriceCell.h"

#import "PaiRecordViewController.h"

@interface PaiRecordViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property (nonatomic, strong) NSMutableArray<UserPaiPriceModel *> *dataArray;

@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, assign) NSInteger pageNum;

@end

@implementation PaiRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self request_BidRecordData];
}

- (void)request_BidRecordData {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.goodsInfoModel.itemGoodID forKey:@"aid"];
    
    NSString *path = @"";
    switch (self.type) {
        case PPPaiRecordDetailType_NormalPai:{
            path = TT_AUCTION_BIDRECORD;

        } break;
        case PPPaiRecordDetailType_MallPai:{
            path = TT_SHOP_MALLPAI_RECORD;
            [dic setValue:@"1" forKey:@"curPage"];
            [dic setValue:@"100" forKey:@"pageSize"];

        } break;
        default:
            break;
    }
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,path];
    
    [SVProgressHUD showWithStatus:@"正在请求拍价历史记录信息"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        //返回data为字典
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString * code = [data valueForKey:@"code"];
            //请求成功
            if ([code isEqualToString:@"200"]) {
                
                NSArray *resultArray = [data valueForKey:@"data"];
                if ([resultArray isKindOfClass:[NSArray class]]) {

                    for (int i = 0; i<resultArray.count; i++) {
                        
                        NSDictionary *paiPriceDic = resultArray[i];
                        UserPaiPriceModel *model = [[UserPaiPriceModel alloc] init];
                        model.userName = [paiPriceDic valueForKey:@"userName"];
                        model.userIcon = [paiPriceDic valueForKey:@"avatar"];
                        model.itemTime = [paiPriceDic valueForKey:@"actionTimeStr"];
                        
                        NSString *userId = [paiPriceDic valueForKey:@"userId"];
                        
                        if (![userId isKindOfClass:[NSString class]]) {
                            userId = [NSString stringWithFormat:@"%@",userId];
                        }
                        if (![userId isEqualToString:[TTUserModel sharedObject].userID]) {
                            model.messageType = PP_Pai_MessageType_User;
                        } else {
                            model.messageType = PP_Pai_MessageType_Mine;
                        }
                        model.price = [[paiPriceDic valueForKey:@"curPrice"] doubleValue];
                        model.bounty = [[paiPriceDic valueForKey:@"bounty"] doubleValue];
                        [self.dataArray addObject:model];
                    }
                    
                    if (resultArray.count > 0) {
                        [[TTNoResultManager shareManager] hideNoResultWithView:self.tableview];
                    } else {
                        [[TTNoResultManager shareManager] showNoResultWithStateInView:self.tableview errorCode:kTTViewStateNoData btnTitle:@"返回" didPressButton:^{
                            [self.navigationController popViewControllerAnimated:YES];
                        }];
                    }
                }
                
            } else {
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"msg"]];
                [SVProgressHUD dismissWithDelay:1];
            }
            //刷新
        } else {
            //返回为空
            [SVProgressHUD showErrorWithStatus:@"返回为空"];
            [SVProgressHUD dismissWithDelay:1];
        }
        
        [self.tableview reloadData];
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [[TTNoResultManager shareManager] showNoResultWithStateInView:self.tableview errorCode:error.code btnTitle:@"返回" didPressButton:^{
            [self.navigationController popViewControllerAnimated:YES];
            
        }];
    }];
    
}

- (void)initView {

    self.title = @"出价记录";
    
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.estimatedRowHeight = 0;
    [self.tableview registerNib:[UINib nibWithNibName:@"UserPaiPriceCell" bundle:nil] forCellReuseIdentifier:@"UserPaiPriceCellID"];
    [self.tableview registerNib:[UINib nibWithNibName:@"MyPaiPriceCell" bundle:nil] forCellReuseIdentifier:@"MyPaiPriceCellID"];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (self.dataArray[indexPath.row].messageType) {
            
        case PP_Pai_MessageType_Mine:{
            
            MyPaiPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyPaiPriceCellID" forIndexPath:indexPath];
            
            if (self.dataArray.count > indexPath.row) {
                [cell fillCellWithModel:self.dataArray[indexPath.row]];
            }
            
            return cell;
        } break;
            
        case PP_Pai_MessageType_User:{
            UserPaiPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UserPaiPriceCellID" forIndexPath:indexPath];
            
            if (self.dataArray.count > indexPath.row) {
                [cell fillCellWithModel:self.dataArray[indexPath.row]];
            }
            return cell;
        }
        default:{
            
            UserPaiPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UserPaiPriceCellID" forIndexPath:indexPath];
            
            if (self.dataArray.count > indexPath.row) {
                [cell fillCellWithModel:self.dataArray[indexPath.row]];
            }
            return cell;
        }
            break;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSMutableArray<UserPaiPriceModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray new];
    }
    return _dataArray;
}

@end
